package core;

public class PeaceBoard extends Board {
    public PeaceBoard(String player1Name, String player2Name) {
        super(player1Name, player2Name);
    }

    @Override
    protected void initializeBoard() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                grid[i][j] = EMPTY;
            }
        }
    }

    @Override
    public boolean makeMove(int row, int col) {
        if (gameOver || !isValidPosition(row, col) || grid[row][col] != EMPTY) {
            return false;
        }

        grid[row][col] = currentPlayer.getPiece();

        // 检查游戏是否结束(棋盘是否已满)
        gameOver = isBoardFull();

        if (!gameOver) {
            switchPlayer();
        }

        return true;
    }

    @Override
    public void display() {
        System.out.println("  A B C D E F G H");
        System.out.println("  -----------------");

        for (int i = 0; i < 8; i++) {
            System.out.print((i + 1) + " |");
            for (int j = 0; j < 8; j++) {
                System.out.print(grid[i][j] + " ");
            }
            System.out.print("|");

            // 显示玩家信息
            if (i == 2) {
                System.out.print("  游戏 " + (GameManager.getCurrentGameIndex() + 1));
            } else if (i == 3) {
                System.out.print("  " + player1.getName() +
                        (currentPlayer == player1 ? " " + BLACK : ""));
            } else if (i == 4) {
                System.out.print("  " + player2.getName() +
                        (currentPlayer == player2 ? " " + WHITE : ""));
            }

            System.out.println();
        }
        System.out.println("  -----------------");
    }

    private boolean isBoardFull() {
        for (int i = 0; i < 8; i++) {
            for (int j = 0; j < 8; j++) {
                if (grid[i][j] == EMPTY) {
                    return false;
                }
            }
        }
        return true;
    }
}